/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.KindOfSubjectDAL;
import mnstudent.datatranferobject.KindOfSubjectDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author 11520_000
 */
public class KindOfSubjectBLL {
    
    KindOfSubjectDAL kindofsubjectDAL;
    
    public KindOfSubjectBLL()
    {
        kindofsubjectDAL = new KindOfSubjectDAL();
    }
    
    /**
     * Ham lay tat ca cac LOAIMONHOC
     *
     * @return boolean
     */
    public ResultSet getAllKindOfSubject() {
      
        return kindofsubjectDAL.getAllKindOfSubject();
    }
/**
     * Ham lay LOAIMONHOC theo ma LOAIMONHOC
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getKindOfSubjectById(String _id)
    {
      
        return kindofsubjectDAL.getKindOfSubjectById(_id);
    }
    
     /**
     * ham them moi LOAIMONHOC xuong database
     * @param _kindofsubject
     * @return true: success, false: fail
     */
    public boolean insertKindOfSubjectToDatabase(KindOfSubjectDTO _kindofsubject)
    {
       return kindofsubjectDAL.insertKindOfSubjectToDatabase(_kindofsubject);
    }
    
    /**
     * Ham cap nhat sua doi thong tin LOAIMONHOC
     * @param _kindofsubject
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateKindOfSubjectToDatabase(KindOfSubjectDTO _kindofsubject)
    {
        return kindofsubjectDAL.updateKindOfSubjectToDatabase(_kindofsubject);
    }
    
    /**
     * Ham xoa mot LOAIMONHOC ra khoi database
     * @param _kindofsubject
     * @return 
     */
     public boolean deleteKindOfSubjectFromDatabase(KindOfSubjectDTO _kindofsubject)
    {       
        return kindofsubjectDAL.deleteKindOfSubjectFromDatabase(_kindofsubject);
    }
     
     public ArrayList<KindOfSubjectDTO> getAllKindOfSubjectToArray()
    {
        ArrayList<KindOfSubjectDTO> temp = new ArrayList<>();
        ResultSet rs = getAllKindOfSubject();
        try {
            while(rs.next())
            {
                KindOfSubjectDTO kindofsubject = new KindOfSubjectDTO(rs.getNString(1), rs.getNString(2), rs.getInt(3), rs.getInt(4));
                temp.add(kindofsubject);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;   
    }
     
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
    {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
}
